import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

final textStyle = TextStyle(color: Color.fromRGBO(255, 255, 255, 1),decoration: TextDecoration.none,fontSize: 15.sp);
class CloumnChart extends StatefulWidget{

  List<Map>? dataList;

  String title;

  CloumnChart({Key? key,required this.dataList,required this.title}):super(key: key);
  @override
  _CloumnChartState createState() => _CloumnChartState();

}

class _CloumnChartState extends State<CloumnChart>{

  @override
  Widget build(BuildContext context) {
    var isCardView = false;
    late TooltipBehavior _tooltipBehavior;
    _tooltipBehavior = TooltipBehavior(enable: true);
    return SfCartesianChart(
      plotAreaBackgroundColor: Colors.transparent,
      plotAreaBorderWidth: 0,
      title: ChartTitle(text: '车间信息',alignment: ChartAlignment.near,textStyle: TextStyle(fontSize: 1.0,color: Colors.transparent)),
      primaryXAxis: CategoryAxis(
        labelStyle: textStyle,
        majorGridLines: MajorGridLines(width: 0),
      ),
      palette: <Color>[
        Colors.orange,
        Colors.deepOrangeAccent,
        Colors.teal,
        Colors.yellowAccent,
        Colors.blueAccent
      ],
      primaryYAxis: NumericAxis(
          // maximum: 35,
          // minimum: 0,
          // interval: 5,
          axisLine: AxisLine(width: 0),
          majorTickLines: MajorTickLines(size: 0),
          labelStyle: textStyle,
      ),
      series: _getDefaultColumn(),
      legend: Legend(isVisible: !isCardView,position: LegendPosition.top,textStyle: textStyle),
      tooltipBehavior: _tooltipBehavior,
    );
  }
  List<ColumnSeries<ChartSampleData, String>> _getDefaultColumn() {
    final List<List<ChartSampleData>> chartData = widget.dataList!.map((e) => [ChartSampleData(widget.title,e['y'])]).toList();
    final List fixedList = Iterable<int>.generate(chartData.length).toList();
    return fixedList.map((i) => ColumnSeries<ChartSampleData, String>(
      /// To apply the column width here.
      //   width: isCardView ? 0.8 : _columnWidth,
      //
      //   /// To apply the spacing betweeen to two columns here.
      //   spacing: isCardView ? 0.2 : _columnSpacing,
        spacing: 0.2 ,
        dataSource: chartData[i],
        // color: const Color.fromRGBO(33,205,10, 1),
        xValueMapper: (ChartSampleData sales, _) => sales.x,
        yValueMapper: (ChartSampleData sales, _) => sales.y,
        dataLabelSettings: DataLabelSettings(
          // Renders the data label
            isVisible: true
        ),
        name: widget.dataList![i]['x']),).toList();
  }
}

///Chart sample data
class ChartSampleData {
  /// Holds the datapoint values like x, y, etc.,
  ChartSampleData(
      this.x,
        this.y,
        );

  /// Holds x value of the datapoint
  final String? x;

  /// Holds y value of the datapoint
  final num? y;
}